package org.gestion.bp.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.gestion.bp.entities.Client;
import org.gestion.bp.entities.Compte;
import org.gestion.bp.entities.Employe;
import org.gestion.bp.entities.Groupe;
import org.gestion.bp.entities.Operation;

public class BanqueDaoImpl implements IBanqueDao {
	
	@PersistenceContext
	private EntityManager em;

	@Override
	public Client addClient(Client cli) {
		em.persist(cli);
		return cli;
	}

	@Override
	public Employe addEmploye(Employe emp, Long codeSup) {
		if (codeSup != null)  {
			Employe sup = em.find(Employe.class, codeSup);
			emp.setEmployeSup(sup);
		}
		em.persist(emp);
		return emp;
	}

	@Override
	public Groupe addGroupe(Groupe grp) {
		em.persist(grp);
		return grp;
	}

	@Override
	public void addEmployeToGroup(Long codeEmp, Long codeGrp) {
		Employe emp = em.find(Employe.class, codeEmp);
		Groupe grp = em.find(Groupe.class, codeGrp);
		emp.getGroupes().add(grp);
		grp.getEmployes().add(emp);
	}

	@Override
	public Compte addCompte(Compte cpte, Long codeCli, Long codeEmp) {
		Client cli = em.find(Client.class, codeCli);
		Employe emp = em.find(Employe.class, codeEmp);
		cpte.setClient(cli);
		cpte.setEmploye(emp);
		em.persist(cpte);
		return cpte;
	}

	@Override
	public Operation addOperation(Operation op, String codeCpte, Long codeEmp) {
		Compte cpte = em.find(Compte.class, codeCpte);
		Employe emp = em.find(Employe.class, codeEmp);
		op.setCompte(cpte);
		op.setEmploye(emp);
		em.persist(op);
		return op;
	}

	@Override
	public Compte consulterCompte(String codeCpte) {
		Compte cpte = em.find(Compte.class, codeCpte);
		if (cpte == null) {
			throw new RuntimeException("Compte inexistant");
		}
		return cpte;
	}

	@Override
	public List<Operation> consulterOperations(String codeCpte) {
		Query req = em.createQuery("select o from Operation o where o.compte.codeCompte = :x");
		req.setParameter("x", codeCpte);
		return req.getResultList();
	}

	@Override
	public Client consulterClient(Long codeCli) {
		Client cli = em.find(Client.class, codeCli);
		if (cli == null) {
			throw new RuntimeException("Client inexistant");
		}
		return cli;
	}

	@Override
	public List<Client> consulterClients(String motCle) {
		Query req = em.createQuery("select c from Client c where c.nomClient like :x");
		req.setParameter("x", "%" + motCle + "%");
		return req.getResultList();
	}

	@Override
	public List<Compte> getComptesByClient(Long codeCli) {
		Query req = em.createQuery("select c from Compte c where c.client.codeClient = :x");
		req.setParameter("x", codeCli);
		return req.getResultList();
	}

	@Override
	public List<Compte> getComptesByEmploye(Long codeEmp) {
		Query req = em.createQuery("select c from Compte c where c.employe.codeEmploye = :x");
		req.setParameter("x", codeEmp);
		return req.getResultList();
	}

	@Override
	public List<Employe> getEmployes() {
		Query req = em.createQuery("select e from Employe e");
		return req.getResultList();
	}

	@Override
	public List<Groupe> getGroupes() {
		Query req = em.createQuery("select g from Groupe g");
		return req.getResultList();
	}

	@Override
	public List<Employe> getEmployesByGroupe(Long codeGrp) {
		Query req = em.createQuery("select e from Employe e where e.groupes.codeGroupe = :x");
		req.setParameter("x", codeGrp);
		return req.getResultList();
	}
}
